﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IllustratedCsharp7_ch8_override
{
    class chapt8_6OverrideMethod
    {
        static void _Main()
        {
            SecodDerived derived = new SecodDerived();
            MyBaseClass mybc = (MyBaseClass)derived;
            derived.Print();
            mybc.Print();
        }       
    }
    class MyBaseClass                    //基类
    {

        virtual public void Print()     //虚方法
        {
            Console.WriteLine("This is the base class.");
        }
    }

    class MyDerivedClass : MyBaseClass //派生类
    {
        public int var1;

        override public void Print()   //覆盖方法
        {
            Console.WriteLine("This is the derived class");
        }
    }
    /* 使用 override声明Print
     *结果是：无论Print是通过派生类调用还是通过基类调用，都会调用最高派生类中的方法。
     *通过基类调用时，调用沿着继承层次向上传递。*/
    //class SecodDerived : MyDerivedClass
    //{
    //    public override void Print()
    //    {
    //        Console.WriteLine("This is the second derived class.");
    //    }
    //}


    /// <summary>
    /// 使用new声明Print
    /// 
    /// </summary>
    class SecodDerived : MyDerivedClass
    {
        new public void Print()
        {
            Console.WriteLine("This is the second derived class.");
        }
    }
}
